// 打开App
function open_app(appName){
    var name = getPackageName(appName);
    if(name){
        toastLog("打开"+appName+"...");
        launch(name);
    }
    else{
        toastLog("App名称错误");
    }
    return name;
}

open_app("京东");

// 茅台预约,京东抢购茅台需要提前预约,
// 目前预约时间是工作日上午10点到10点半之间,
// 这里的逻辑是将京东茅台添加到收藏中,
// 然后进入App后点击"我的",点击"商品收藏",点击"飞天茅台",
// 点击"立即预约",下面是相关代码

// 等待View存在并延时点击
function wait_view_exists_click(viewName, delayTime, showMsg){
    while(true){
        if(className("android.view.View").desc(viewName).exists()){
            sleep(delayTime);
            toastLog("点击"+showMsg)
            className("android.view.View").desc(viewName).findOnce().click();
            break;
        }
    }
}

// 等待TextView存在并延时点击
function wait_textview_exists_click(textViewName, delayTime, showMsg){
    while(true){
        if(className("android.widget.TextView").text(textViewName).exists()){
            sleep(delayTime);
            toastLog("点击" + showMsg)
            let b = className("android.widget.TextView").text(textViewName).findOnce().bounds();
            click(b.centerX(),b.centerY());
            break;
        }
    }
}

// 等待TextView存在并延时点击, 如果满足条件退出循环
function wait_textview_exists_click_break(textViewName, delayTime, showMsg, textViewNameBreak, showMsgBreak){
    while(true){
        if(className("android.widget.TextView").text(textViewName).exists()){
            sleep(delayTime);
            toastLog("点击" + showMsg)
            let b = className("android.widget.TextView").text(textViewName).findOnce().bounds();
            click(b.centerX(),b.centerY());
            break;
        }
        else if(className("android.widget.TextView").text(textViewNameBreak).exists())
        {
            toastLog(showMsgBreak)
            break;
        }
    }
}

// 等待Button存在并点击
function wait_button_exists_click(buttonName, delayTime, showMsg){
    while(true){
        if(className("android.widget.Button").text(buttonName).exists()){
            sleep(delayTime);
            toastLog("点击" + showMsg)
            let b = className("android.widget.Button").text(buttonName).findOnce().bounds();
            click(b.centerX(),b.centerY());
            break;
        }
    }
}

// 京东抢购茅台预约
function jd_mt_yy(){
    var name = open_app("京东");
    waitForPackage(name);

    wait_view_exists_click("我的", 3000, "\"我的\"");
    wait_textview_exists_click("商品收藏", 3000, "\"商品收藏\"");
    wait_textview_exists_click("飞天 53% vol  500ml 贵州茅台酒 （带杯） ", 3000, "\"飞天茅台\"");
    wait_textview_exists_click_break("立即预约", 3000, "\"立即预约\"", "已预约", "预约成功");
    wait_button_exists_click("知道啦", 3000, "\"知道啦\"");

    toastLog("预约成功")
}

jd_mt_yy()


// 京东抢购茅台预约
function jd_mt_qg(){
    wait_textview_exists_click("立即抢购", 0, "\"立即抢购\"");
    wait_button_exists_click("提交订单", 0, "\"提交订单\"");
}
// 抢购不太可能一次成功的,我们这里设置每天10点02分预约,12:00:00整时开抢

// 定时预约抢购
function timing_jd_mt(hour_yy, min_yy, sec_yy, hour_qg, min_qg, sec_qg){
    var yy_flag = 0;    //预约成功标志
    toastLog("执行京东抢购任务...");
    while(true){
        var nowDate = new Date();
        if(nowDate.getHours()==hour_yy && nowDate.getMinutes()==min_yy && nowDate.getSeconds()==sec_yy){
            toastLog("定时时间到, 开始预约...");
            jd_mt_yy();
            yy_flag = 1;
        }
        if(nowDate.getHours()>=hour_qg && nowDate.getMinutes()>=min_qg && nowDate.getSeconds()>=sec_qg &&yy_flag){
            toastLog("定时时间到, 开始抢购...");
            jd_mt_qg();
        }
    }
}

timing_jd_mt(10, 02, 00, 12, 00, 00);
